package com.working.user

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Looper
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.working.libbase.base.BaseActivity
import com.working.user.databinding.UserActivityMainBinding
import com.working.user.databinding.UserItemDataBinding
import com.working.user.model.DateBean
import com.working.user.widget.MItemDecoration

class UserActivityMainActivity : BaseActivity() {
    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, UserActivityMainActivity::class.java)
            context.startActivity(starter)
        }
    }

    private val mBinding by binding<UserActivityMainBinding>(R.layout.user_activity_main)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding.apply { lifecycleOwner = this@UserActivityMainActivity }
        val adapter = DataAdapter()
        mBinding.datas.adapter = adapter
        mBinding.datas.addItemDecoration(MItemDecoration())
        adapter.submitList(DateBean.getMockData())
    }


    inner class DataAdapter :
        ListAdapter<DateBean, DataAdapter.ViewHolder>(object : DiffUtil.ItemCallback<DateBean>() {
            override fun areItemsTheSame(oldItem: DateBean, newItem: DateBean): Boolean {
                return oldItem.date == newItem.date
            }

            @SuppressLint("DiffUtilEquals")
            override fun areContentsTheSame(oldItem: DateBean, newItem: DateBean): Boolean {
                return oldItem == newItem
            }

        }) {
        inner class ViewHolder(private val mBinding: UserItemDataBinding) :
            RecyclerView.ViewHolder(mBinding.root) {
            fun bind(data: DateBean?) {
                mBinding.item = data
                mBinding.executePendingBindings()
            }

        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(
                UserItemDataBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.bind(getItem(position))
        }

    }
}